package network;

import helper.Helper;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;

public class ConnectionFactory {
    private static ConnectionFactory instance;
    private BidiMap connectionBaseMap;

    private ConnectionFactory() {
        connectionBaseMap = new TreeBidiMap();
    }

    public static ConnectionFactory instance() {
        if (instance == null) {
            instance = new ConnectionFactory();
        }
        return instance;
    }

    public <T extends ConnectionBase> ConnectionBase create(Class<T> c) {
        T ins = null;
        try {
            ins = c.getDeclaredConstructor().newInstance();
            String id = Helper.genUUIDString();
            ins.setId(id);
            connectionBaseMap.put(id, ins);
        }
        catch (Exception e) {
            System.out.println(e.toString());
        }
        return ins;
    }

    public void remove(ConnectionBase c) {
        if (connectionBaseMap.containsValue(c)) {
            connectionBaseMap.removeValue(c);
        }
    }
}
